<?php

namespace plurious\modules\login;

use framework\engine\Controller;
use framework\Front;

class Login extends Controller {

    // DO NOT cache your authentication view
    protected $isCachable = false;

    public function index() {
        parent::__construct();
        $this->getLanguage('login');
        $isLoggedin = $this->user->isLoggedIn();

        if ($isLoggedin) {
            $this->setTemplate('logout');
            $this->setupLogout();
        } else {
            $this->setupLogin();
        }
        $this->render();
    }

    private function setupLogin() {
        $this->registry = Front::getInstance();
        $this->session = $this->registry->get('session');
        // We must seed the log in form
        $this->data['seedValue'] = $this->session->get('seed');
        // Set error to empty string until needed
        $this->data['login_error'] = $this->session->get('login_message');
        $this->data['login_heading_title'] = $this->getText('heading_title');
        $this->data['login_url'] = 'home/login';
        // Remember to set up the config options in the auth_config.php file
        // Optionally, you can set or override them here
        $this->data['UserNameCol'] = $this->config->get('UserNameCol');
        $this->data['PasswordCol'] = $this->config->get('PasswordCol');
        $this->data['seedLabel'] = $this->config->get('seedLabel');
        // Establishes session based login or longterm login based upon
        // your auth_config.php settings
        $remember = $this->config->get('auth_offer_remember');
        if($this->config->get('auth_offer_remember')){
            $this->data['rememberMe'] = $this->config->get('RememberMe');    
        }
        // Use a password modification technique for enhanced security
        $enhance = $this->config->get('auth_password_enhance');
        if ($enhance) {
            $this->data['instruction_label'] = 'Instructions:';
            $this->data['instructions'] = $this->session->get('pw_enhance_output');
        }
        // Set up a nice message
        $this->data['login'] = $this->getText('text_welcome', array('register'));
    }

    private function setupLogout() {
        // We must seed the log in form
        $this->data['seedValue'] = $this->session->get('seed');
        // Set error to empty string until needed
        $this->data['login_heading_title'] = $this->getText('heading_title');
        $this->data['greeting'] = $this->session->get('login_message') . $this->session->getUserData('nicename');
        $this->data['logout_url'] = 'home/logout';
        $this->data['seedLabel'] = $this->config->get('seedLabel');
        // Set up a nice message
        $user = $this->session->getUserData();
        $nicename = $user['nicename'];
        $this->data['login'] = $this->getText('text_logged', array($this->url->link('profile') . '/' . $nicename, $nicename, $this->url->link('home/logout')));
        $this->data['my_account_link'] = $this->url->link('admin/myaccount');
    }

}

?>
